package com.example.myapplication.JavaParser

import com.google.common.base.CaseFormat
import java.io.File
import java.io.FileWriter


val layouts =
    "C:\\Users\\DELL\\AndroidStudioProjects\\overseas_android\\im\\src\\main\\res\\layout"

val appKotlin =
    "C:\\Users\\DELL\\AndroidStudioProjects\\overseas_android\\im\\src\\main\\java\\com\\hylife\\im"

val drawable =
    "C:\\Users\\DELL\\AndroidStudioProjects\\overseas_android\\im\\src\\main\\res\\drawable"

val mipmap =
    "C:\\Users\\DELL\\AndroidStudioProjects\\overseas_android\\im\\src\\main\\res\\mipmap-xxhdpi"

fun main(args: Array<String>) {
    LoadFiles(false).printDirByRecursives4(File(appKotlin), 0)
    LoadFiles(true).printDirByRecursives4(File(appKotlin), 0)
}

class LoadFiles(val isSelf: Boolean) {
    fun printDirByRecursives(dir: File, level: Int) {
        //获取这个目录下所有的子文件和子目录的数组。
        val files = dir.listFiles()
        //遍历这个数组，取出每个File对象
        if (files != null) {
            for (f in files) {
                //判断这个File是否是一个文件，是：
                if (f.isFile) {
                    val name = Remodeling().parseKotlinFile(f)
                    if (name.isNotEmpty() && !name.contains("Im") && !name.contains("IM")) {
                        val dest: File = File(dir.absolutePath + "/Im" + f.name)
                        f.renameTo(dest)
//                        printDirByRecursives2(File(appKotlin), 0, name)
                    }
                } else { //否则就是一个目录，继续递归
                    //递归调用
                    printDirByRecursives(f, level + 1)
                }
            }
        }
    }

    fun printDirByRecursives2(dir: File, level: Int, name: String, className: String) {
        //获取这个目录下所有的子文件和子目录的数组。
        val files = dir.listFiles()
        //遍历这个数组，取出每个File对象
        if (files != null) {
            for (f in files) {
                //判断这个File是否是一个文件，是：
                if (f.isFile) {
                    Remodeling().parseKotlinFile6(f, name, className)
                } else { //否则就是一个目录，继续递归
                    //递归调用
                    printDirByRecursives2(f, level + 1, name, className)
                }
            }
        }
    }

    fun printDirByRecursives3(dir: File, level: Int) {
        //获取这个目录下所有的子文件和子目录的数组。
        val files = dir.listFiles()
        //遍历这个数组，取出每个File对象
        if (files != null) {
            for (f in files) {
                //判断这个File是否是一个文件，是：
                if (f.isFile) {
                    val name = f.name
                    val tag1 = f.name.indexOf("_")
                    if (!name.contains("im") && tag1 > 0) {
                        val start = f.name.substring(0, tag1 + 1)
                        val end = f.name.substring(tag1 + 1)
                        val dest: File = File(dir.absolutePath + "/" + start + "im_" + end)
                        f.renameTo(dest)
                        val layout = name.substring(0, name.indexOf("."))
//                        printDirByRecursives2(File(appKotlin), 0, layout)
//                        printDirByRecursives2(File(layouts), 0, layout)
                    }
                } else { //否则就是一个目录，继续递归
                    //递归调用
                    printDirByRecursives3(f, level + 1)
                }
            }
        }
    }

    fun printDirByRecursives4(dir: File, level: Int) {
        //获取这个目录下所有的子文件和子目录的数组。
        val files = dir.listFiles()
        //遍历这个数组，取出每个File对象
        if (files != null) {
            for (f in files) {
                //判断这个File是否是一个文件，是：
                if (f.isFile && !f.name.contains("Bean") && !f.name.contains("Entity") && !f.name.contains(
                        "Contract"
                    )
                    && !f.name.contains("GroWing")
                ) {
                    if (f.name.endsWith(".kt")) {
                        Remodeling().parseKotlinFile4(f,isSelf)
                    } else if (f.name.endsWith(".java")) {
                        Remodeling().parseKotlinFile5(f,isSelf)
                    }
                } else { //否则就是一个目录，继续递归
                    //递归调用
                    printDirByRecursives4(f, level + 1)
                }
            }
        }
    }

}


class Remodeling {
    fun parseKotlinFile(file: File): String {
        val codeList = file.readLines().toMutableList()
        val bindings = mutableListOf<String>()
        val className = "class "
        codeList.filter { it.contains(className) }.forEach {
            val layout = it.substring(it.indexOf(className) + className.length).let { it2 ->
                try {
                    it2.substring(0, it2.indexOf(" "))
                } catch (e: Exception) {
                    ""
                }
            }
            return layout
        }
        return ""
    }

    fun parseKotlinFile2(file: File, name: String) {
        val codeList = file.readLines().toMutableList()
        codeList.forEachIndexed { index, s ->
            val binding = filesFormat(name) + "Binding"
            if (s.contains(name)) {
                val tag1 = name.indexOf("_")
                val start = name.substring(0, tag1 + 1)
                val end = name.substring(tag1 + 1)
                codeList[index] = s.replace(name, start + "im_" + end)

            } else if (s.contains(binding)) {
                val tag1 = name.indexOf("_")
                val start = name.substring(0, tag1 + 1)
                val end = name.substring(tag1 + 1)
                val result = filesFormat(start + "im_" + end) + "Binding"
                codeList[index] = s.replace(binding, result)
                println()
            }
        }

        val stringBuilder = StringBuilder()
        codeList.forEach {
            stringBuilder.appendLine(it)
        }
        val writer = FileWriter(file.absolutePath)
        writer.write(stringBuilder.toString())
        writer.close()
    }

    /**
     * 替换drawable
     */
    fun parseKotlinFile3(file: File, name: String) {
        val codeList = file.readLines().toMutableList()
        codeList.forEachIndexed { index, s ->
            if (s.contains(name)) {
                val tag1 = name.indexOf("_")
                val start = name.substring(0, tag1 + 1)
                val end = name.substring(tag1 + 1)
                codeList[index] = s.replace(name, start + "im_" + end)
            }
        }

        val stringBuilder = StringBuilder()
        codeList.forEach {
            stringBuilder.appendLine(it)
        }
        val writer = FileWriter(file.absolutePath)
        writer.write(stringBuilder.toString())
        writer.close()

    }

    fun parseKotlinFile4(file: File, isSelf: Boolean) {
        val codeList = file.readLines().toMutableList()
        codeList.forEachIndexed { index, s ->
            if (s.contains("fun ") && !s.contains("override") && fileter(s)) {
                val tag1 = s.indexOf("fun ")
                val str1 = s.substring(tag1 + "fun ".length)
                val tag2 = str1.indexOf("(")
                if (tag2 >= 0) {
                    val str2 = str1.substring(0, tag2)
                    codeList[index] = s.replace(str2, str2 + "Im")
                    if (!isSelf) {
                        LoadFiles(false).printDirByRecursives2(File(appKotlin), 0, str2, file.name)
                    }
                }
            }
        }
        if (isSelf) {
            val stringBuilder = StringBuffer()
            codeList.forEach {
                stringBuilder.appendLine(it)
            }
            val writer = FileWriter(file.absolutePath)
            writer.write(stringBuilder.toString())
            writer.close()
        }
    }

    fun parseKotlinFile5(file: File, isSelf: Boolean) {
        val codeList = file.readLines().toMutableList()
        var lastStr = ""
        codeList.forEachIndexed { index, s ->
            if (s.contains("void ") && !lastStr.contains("Override") && fileter(s)) {
                val tag1 = s.indexOf("void ")
                val str1 = s.substring(tag1 + "void ".length)
                val tag2 = str1.indexOf("(")
                if (tag2 >= 0) {
                    val str2 = str1.substring(0, tag2)
                    codeList[index] = s.replace(str2, str2 + "Im")
                    if (!isSelf) {
                        LoadFiles(false).printDirByRecursives2(File(appKotlin), 0, str2, file.name)
                    }
                }
            }
            lastStr = s
        }
        if (isSelf) {
            val stringBuilder = StringBuffer()
            codeList.forEach {
                stringBuilder.appendLine(it)
            }
            val writer = FileWriter(file.absolutePath)
            writer.write(stringBuilder.toString())
            writer.close()
        }
    }

    fun parseKotlinFile6(file: File, name: String, className: String) {
        val codeList = file.readLines().toMutableList()
        var isHasClass = false
        val className2 = className.substring(0, className.indexOf("."))
        codeList.forEachIndexed { index, s ->
            if (s.contains(className2) || file.name == className) {
                isHasClass = true
            }
        }
        if (isHasClass) {
            codeList.forEachIndexed { index, s ->
                if (className2 == "ImChatActivity" && name == "updateData") {
                    println()
                }
                if (!s.contains("void ") && !s.contains("fun ") && !s.contains("override") && fileter(
                        s
                    )
                ) {
                    if (s.contains(name + "(")   || s.contains(name + "{")) {
                        codeList[index] = s.replace(name, name + "Im")
                    }
                }
            }
            val stringBuilder = StringBuffer()
            codeList.forEach {
                stringBuilder.appendLine(it)
            }
            val writer = FileWriter(file.absolutePath)
            writer.write(stringBuilder.toString())
            writer.close()
        }
    }

    fun fileter(name: String): Boolean {
        return !name.contains("setOnClickListener") && !name.contains("getInstance") && !name.contains(
            "show"
        ) && !name.contains("Service")
                && !name.contains("abstract") && !name.contains("Contract") && !name.contains("GroWing")
                && !name.contains("Presenter")
    }

    fun filesFormat(str: String): String {
        if (str.contains("_")) {
            return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, str)
        }
        return str;
    }

}


